<?php
/**
 * Controller_Article
 *
 * Класс Controller_Article отвечает за обработку раздела Статьи
 *
 * @author Дмитрий Кузьма <kouzma.d@gmail.com>
 * @version 1.0
 */
class Controller_Article extends Controller_Base {


	public function __construct(&$arrRequest) {
		$objArticle = new Article();
    $this->setCrumbs(array(
      array('url' => W_ABSPATH, 'name' => 'Хлебные крошки')
    ));

		parent::__construct($arrRequest);
		switch ($this->arrRequest[1]) {
		  case '':
		  	if (!SimpleAdministration::isAdmin()) $this->show403Error();
   			else $this->getActionListArticles();
   			break;
		  case 'edit':
		  	if (!SimpleAdministration::isAdmin()) $this->show403Error();
   			elseif (isset($arrRequest[2]) && is_numeric($arrRequest[2])) {
		  	  $this->getActionEdit(intval($arrRequest[2]));
		  	}
		  	else $this->show404Error();
				break;
		  case 'add':
		  	if (!SimpleAdministration::isAdmin()) $this->show403Error();
   			else $this->getActionAdd();
				break;
			
		  default: 
		    if (is_numeric($this->arrRequest[1])){
		      $objArticle = new Article(intval($this->arrRequest[1]));
		      if ($objArticle->id) $this->getActionPage($objArticle);
		      else $this->show404Error();
		    }
	      else {
	        $this->show404Error();
	      }
		        
	}
	}
	

	/**
	 * Действие - простой список статей 
	 *
	 */
	public function getActionListArticles(){
		$objArticle = new Article();
  	$arrArticles = $objArticle->find('all', array('select' => '`article`.`ID_article`, `article`.`title`,  `article`.`keywords`, `article`.`description`,`article`.`text_preview`, `article`.`article_name`, `article`.`url`, `article`.`text`',
																												 'order'=>'`created_at` ASC'
																												 ));
    $this->title = 'Статьи';
		$this->pageHead = $this->title;
		if (SimpleAdministration::isAdmin()) $this->pageHead .= ' <a href="' . W_ABSPATH . 'article/add/"><img src="' . W_IMAGES . 'button/add.gif" class="addButton" alt="" /></a>';
		$this->addCrumb($this->title, null, true);
		$this->addJs('faq.js');
		$this->addCss('article.css');
		
		$this->showHeader();
      View_Article::showList($arrArticles);
		$this->showFooter();		
	}
	
	/**
	 * Действие - редактирование Статьи
	 *
	 * @param int $articleId
	 */
	public function getActionEdit($articleId) {
	  $objArticle = new Article($articleId);
		if (isset($_POST['Article'])) {
		  $objArticle1 = new Article($_POST['Article']);
		  if ($articleId != $objArticle->id) User_Session::moveTo(W_ABSPATH, 'Вы редактируете другую статью!');
			if ($objArticle1->save()) {
				User_Session::moveTo(W_ABSPATH . 'article/', 'Статья изменена', 1);
			}
		}
		else {
		  
		}
		$this->addWysiwygMetas();		
		$this->title = 'Редактирование статьи';
		$this->pageHead = $this->title;
	  $this->showHeader();
      View_Article::showForm($objArticle);
	  $this->showFooter();
	}
	
  /**
	 * Действие - добавление Статьи 
	 *
	 */
	public function getActionAdd() {
		if (isset($_POST['Article'])) {
			$objArticle = new Article($_POST['Article']);
			if ($objArticle->save()) User_Session::moveTo(W_ABSPATH . 'article/edit/' . $objArticle->id . '/', 'Статья добавлена', 1);
		}
		else {
			$objArticle = new Article();
		}
		
		$this->addWysiwygMetas();
	  $this->title = 'Добавление статьи';
	  $this->pageHead = $this->title;
		$this->showHeader();
      View_Article::showForm($objArticle);
	  $this->showFooter();
	}

	
  /**
	 * Действие -вывод страницы Статьи 
	 *
	 */	
	

	 
	public function getActionPage($objArticle){
  
	  $this->title = $objArticle->getField('title');
	  $this->keywords = $objArticle->getField('keywords');
	  $this->description = $objArticle->getField('description');
	  $this->pageHead = $objArticle->getField('article_name');
	  $this->addJs('faq.js');
		$this->addCss('article.css');
	  
	  $this->showHeader();
      View_Article::showPage($objArticle);
		$this->showFooter();		
	}
	
	
}
?>